package com.sogoodsoft.web.sys;

import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.sogoodsoft.entity.app.Organization;
import com.sogoodsoft.service.app.OrganizationService;
import com.sogoodsoft.util.DwzMsg;
import com.sogoodsoft.web.CrudActionSupport;
import com.sogoodsoft.web.WebConstants;

@Namespace("/sys")
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "organization.action", type = "redirect"),
	@Result(name = "JSTREE", location = "jstree.jsp")
})
public class OrganizationAction extends CrudActionSupport<Organization> {

	private static final long serialVersionUID = -4052047494894591406L;
	
	private List<Organization> tree;
	
	private OrganizationService orgService;
	
	private Long id;
	private Organization entity;
	
	private Long parentId;
	private Organization parent;
	
	private String action;
	
	private Page<Organization> page = new Page<Organization>(WebConstants.PAGE_SIZE);//每页5条记录
	
	@Override
	public String delete() throws Exception {
		orgService.delete(id);		
		Struts2Utils.renderJson(DwzMsg.success());
		return null;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}
	public String addNew()throws Exception{
		//ActionContext.getContext().getValueStack().push(new Organization() );
		entity = null;
		return INPUT;
	}
	@Override
	public String list() throws Exception {		
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		//设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("odr");
			page.setOrder(Page.ASC);
		}
		
		logger.debug("查询组织结构");
		page = orgService.searchOrganization(page, filters);
		return SUCCESS;
	}

	@Override
	protected void prepareModel() throws Exception {

		if (id != null) {
			entity = orgService.get(id);
		} else {
			entity = new Organization();
		}
		if (parentId != null) {
			parent = orgService.get(parentId);
			entity.setParent(parent);
		}
		
	}

	@Override
	public String save() throws Exception {
		orgService.save(entity);
		if("add".equals(action)){
			return addNew();
		}
		Struts2Utils.renderJson(DwzMsg.successAndX(Struts2Utils.getParameter(DwzMsg.param_navTabId)));
		return null;
	}

	@Override
	public Organization getModel() {
		return entity;
	}
	
	public String jstree(){
		logger.info("输出jsTree");
		tree = orgService.getAll();
		/**
		StringBuffer buffer = new StringBuffer();		
		buffer.append(" <script type=\"text/javaScript\">\n");
		buffer.append(" window.tree = new MzTreeView(\"tree\");\n");
		buffer.append(" tree.icons[\"property\"] = \"property.gif\";\n");
		buffer.append(" tree.icons[\"css\"] = \"collection.gif\";\n");
		buffer.append(" tree.icons[\"event\"] = \"collection.gif\";\n");
		buffer.append(" tree.iconsExpand[\"book\"] = \"bookopen.gif\";\n");
		buffer.append(" tree.setIconPath(\"/infosys/js/tree/TreeView/\");\n");
		for (Organization org : tree) {
			if(org.parent==null){
				buffer.append(" tree.nodes[\"-1_"+org.getId()+"\"] = \"ctrl:true;ctrlName:sc;ctrlChecked:true;text:"+org.getNodeName()+";url:workflow/manage/template.asp\";\n");
			}else{
				buffer.append(" tree.nodes[\""+org.getParentId()+"_"+org.getId()+"\"] = \"ctrl:true;ctrlName:sc;ctrlChecked:true;text:"+org.getNodeName()+";url:workflow/manage/template.asp\";\n");
			}
		}
		buffer.append(" tree.setURL(\"#\");\n");
		buffer.append(" tree.wordLine = false;\n");
		buffer.append(" tree.setTarget(\"main\");\n");
		buffer.append("  document.write(tree.toString());\n");
		buffer.append("  </script>\n");
		Map request = (Map)ActionContext.getContext().get("request");
		request.put("jsstr", buffer.toString());
		**/
		return "JSTREE";
	}

	@SuppressWarnings("rawtypes")
    public List getTree() {
		if(tree==null){
			jstree();
		}
		return tree;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void setTree(List tree) {
		this.tree = tree;
	}

	

	public Page<Organization> getPage() {
		return page;
	}

	public void setPage(Page<Organization> page) {
		this.page = page;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Organization getParent() {
		return parent;
	}

	public void setParent(Organization parent) {
		this.parent = parent;
	}

	
	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	public Organization getEntity() {
		return entity;
	}

	public void setEntity(Organization entity) {
		this.entity = entity;
	}

	
	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public OrganizationService getOrgService() {
		return orgService;
	}

	@Autowired
	public void setOrgService(OrganizationService orgService) {
		this.orgService = orgService;
	}

	
}
